import 'package:permission_handler/permission_handler.dart';

abstract final class PermUtil {
  /// App所需的所有权限
  static Iterable<Permission> get all4App => [
        Permission.camera,
        Permission.photos,
        Permission.location,
        Permission.microphone,
        Permission.notification,
        Permission.storage,
        Permission.accessNotificationPolicy,
        Permission.phone,
        Permission.contacts,
      ];
}

extension PermissionX on Permission {
  /// 获取权限状态
  Future<bool> isAvail({
    /// 如果权限未获取，是否自动申请
    bool autoReq = false,
  }) async {
    final status_ = await status;
    if (status_.isGranted) {
      return true;
    } else if (autoReq) {
      final result = await request();
      return result.isGranted;
    }
    return false;
  }

  String get name {
    switch (this) {
      case Permission.camera:
        return '相机';
      case Permission.photos:
        return '相册';
      case Permission.location:
        return '定位';
      case Permission.microphone:
        return '麦克风';
      case Permission.notification:
        return '通知';
      case Permission.storage:
        return '存储';
      case Permission.accessNotificationPolicy:
        return '通知策略';
      case Permission.phone:
        return '电话';
      case Permission.contacts:
        return '通讯录';
      default:
        return '未定义($value)';
    }
  }

  void openSetting() {
    openAppSettings();
  }
}
